using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Ping_Pong
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        Texture2D bat1;
        Vector2 bat1Position;
        Texture2D bat2;
        Vector2 bat2Position;
        Texture2D ball;
        Vector2 ballPosition;
        SpriteFont font;

        const int batSpeed = 5;
        Vector2 ballSpeed = new Vector2(150, 150);

        int player1Score = 0;
        int player2Score = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

            ballPosition.X = (Window.ClientBounds.Width - ball.Width) / 2;
            ballPosition.Y = (Window.ClientBounds.Height - ball.Height) / 2;

            bat1Position.X = (bat1.Width);
            bat1Position.Y = (Window.ClientBounds.Height - bat1.Height) / 2;

            bat2Position.X = (Window.ClientBounds.Width - bat2.Width *2 );
            bat2Position.Y = (Window.ClientBounds.Height - bat2.Height) / 2;

            
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            bat1 = Content.Load<Texture2D>("pong64");
            bat2 = Content.Load<Texture2D>("pong64");
            ball = Content.Load<Texture2D>("ping8");

            font = Content.Load<SpriteFont>("Font");

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if(keyboard.IsKeyDown(Keys.Up))
            {
                bat1Position.Y -= batSpeed;
            }

            if(keyboard.IsKeyDown(Keys.Down))
            {
                bat1Position.Y += batSpeed;
            }

            if (keyboard.IsKeyDown(Keys.A))
            {
                bat2Position.Y -= batSpeed;
            }

            if (keyboard.IsKeyDown(Keys.Z))
            {
                bat2Position.Y += batSpeed;
            }

            //move the ball
            ballPosition += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            ballPosition.X = MathHelper.Clamp(ballPosition.X, 0, Window.ClientBounds.Width - ball.Width);
            ballPosition.Y = MathHelper.Clamp(ballPosition.Y, 0, Window.ClientBounds.Height - ball.Height);
            bat1Position.X = MathHelper.Clamp(bat1Position.X, 0, Window.ClientBounds.Width - bat1.Width);
            bat1Position.Y = MathHelper.Clamp(bat1Position.Y, 0, Window.ClientBounds.Height - bat1.Height);
            bat2Position.X = MathHelper.Clamp(bat2Position.X, 0, Window.ClientBounds.Width - bat2.Width);
            bat2Position.Y = MathHelper.Clamp(bat2Position.Y, 0, Window.ClientBounds.Height - bat2.Height);

            int maxX = graphics.GraphicsDevice.Viewport.Width - ball.Width;
            int maxY = graphics.GraphicsDevice.Viewport.Height - ball.Height;

            //get the rectangles for collision
            Rectangle bat1Rectangle = new Rectangle((int)bat1Position.X, (int)bat1Position.Y, bat1.Width, bat1.Height);
            Rectangle bat2Rectangle = new Rectangle((int)bat2Position.X, (int)bat2Position.Y, bat2.Width, bat2.Height);
            Rectangle ballRectangle = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ball.Width, ball.Height);
            
            
            
            

            if (ballPosition.X >= maxX || ballPosition.X <= 0)
            {
                Score(ballPosition);
                ballSpeed.X *= -1;
            }
            if (ballPosition.Y >= maxY || ballPosition.Y <= 0)
            {
                ballSpeed.Y *= -1;
            }

            if (bat1Rectangle.Intersects(ballRectangle))
            {
                ballSpeed.Y += 50;
                if (ballSpeed.Y < 0)
                {
                    ballSpeed.X -= 50;
                }
                else
                {
                    ballSpeed.X += 50;
                }
                ballSpeed.X *= -1;
            }
            if (bat2Rectangle.Intersects(ballRectangle))
            {
                ballSpeed.Y += 50;
                if (ballSpeed.Y < 0)
                {
                    ballSpeed.X -= 50;
                }
                else
                {
                    ballSpeed.X += 50;
                }
                ballSpeed.X *= -1;
            }


            base.Update(gameTime);
        }

        public void Score(Vector2 ballpos)
        {
            ballPosition.X = (Window.ClientBounds.Width - ball.Width) / 2;
            ballPosition.Y = (Window.ClientBounds.Height - ball.Height) / 2;
            ballSpeed = new Vector2(150, 150);
            if (ballpos.X >= (Window.ClientBounds.Width - ball.Width))
            {
                player1Score += 1;
            }
            if (ballpos.X < (Window.ClientBounds.Width - ball.Width))
            {
                player2Score += 1;
            }

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(bat1, bat1Position, Color.White);
            spriteBatch.Draw(bat2, bat2Position, Color.White);
            spriteBatch.Draw(ball, ballPosition, Color.White);
            spriteBatch.DrawString(font, player1Score.ToString(), Vector2.Zero, Color.White);
            spriteBatch.DrawString(font, player2Score.ToString(), new Vector2(Window.ClientBounds.Width-12, 0), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
